const { notEmpty, bigCamelCase, smallCamelCase } = require('../utils.js')

module.exports = {
  description: '生成一个带增删改查的列表页面(默认生成在src/views文件夹中)',
  prompts: [
    {
      type: 'input',
      name: 'name',
      message:
        '请输入列表的英文关键字，如“user”(注意：按照连接线的格式输入，并且可以为空，eg: xx-xx)：'
    },
    {
      type: 'input',
      name: 'cname',
      message: '请输入列表的中文关键字，如“用户”：',
      validate: notEmpty('cname')
    },
    {
      type: 'directory',
      name: 'compPath',
      basePath: 'src/views',
      message: '请输入组件所在文件夹路径(根路径是src/views)：',
      validate: notEmpty('compPath')
    },
    {
      type: 'directory',
      name: 'apiPath',
      basePath: 'src/api',
      message: '请输接口所在文件夹路径(根路径是src/api)：',
      validate: notEmpty('apiPath')
    },
    {
      type: 'list',
      name: 'popType',
      message: '新增编辑的方式是弹窗还是抽屉？',
      choices: [
        {
          name: '弹窗',
          value: 'ModalShow',
          checked: false
        },
        {
          name: '抽屉',
          value: 'DrawerShow',
          checked: true
        }
      ],
      validate: notEmpty('popType')
    }
  ],
  actions: (promptsData) => {
    Object.keys(promptsData).forEach((key) => {
      promptsData[key] = promptsData[key].trim()
    })
    const { name, cname, compPath, apiPath, popType } = promptsData

    const fileName = compPath.split('/').slice(-1)[0]
    const bigName = bigCamelCase(name || fileName)
    const smallName = smallCamelCase(name || fileName)
    const isDrawer = popType === 'DrawerShow'
    const isModal = popType === 'ModalShow'

    const data = {
      name: name || fileName,
      bigName,
      smallName,
      cname,
      compPath,
      apiPath,
      fileName,
      popType,
      isDrawer,
      isModal
    }

    const actions = [
      // index.vue文件
      {
        type: 'add',
        path: `src/views/${compPath}/index.vue`,
        templateFile: 'plop-templates/crud-page/index.hbs',
        data
      },
      // hooks中的index.ts文件
      {
        type: 'add',
        path: `src/views/${compPath}/hooks/index.ts`,
        templateFile: 'plop-templates/crud-page/hooks-template/index.hbs',
        data
      },
      // hooks中的api.ts文件
      {
        type: 'add',
        path: `src/views/${compPath}/hooks/use${bigName}Api.ts`,
        templateFile: 'plop-templates/crud-page/hooks-template/api.hbs',
        data
      },
      // hooks中的pro-form.tsx文件
      {
        type: 'add',
        path: `src/views/${compPath}/hooks/use${bigName}ProForm.tsx`,
        templateFile: 'plop-templates/crud-page/hooks-template/pro-form.hbs',
        data
      },
      // hooks中的pro-table.tsx文件
      {
        type: 'add',
        path: `src/views/${compPath}/hooks/use${bigName}ProTable.tsx`,
        templateFile: 'plop-templates/crud-page/hooks-template/pro-table.hbs',
        data
      },
      // src/api文件的接口文件
      {
        type: 'add',
        path: `src/api/${apiPath}/${fileName}.ts`,
        templateFile: 'plop-templates/crud-page/api-template/index.hbs',
        data
      },
      // src/api文件的model文件
      {
        type: 'add',
        path: `src/api/model/${fileName}-model.ts`,
        templateFile: 'plop-templates/crud-page/api-template/model.hbs',
        data
      }
    ]

    return actions
  }
}
